package m.app.util;

import com.alibaba.fastjson.JSONObject;
import m.base.model.AppInfo;
import m.common.model.util.ModelUpdateUtil;
import m.system.cache.CacheUtil;
import m.system.exception.MException;
import m.system.util.HttpRequestUtil;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import m.system.util.SystemUtil;

import java.io.File;
import java.util.Date;

public class WxServerApiUtil {
	private static WxServerApiUtil util;
	public static WxServerApiUtil instance() throws Exception{
		if(null==util) {
			util=new WxServerApiUtil();
		}
		return util;
	}
	private AppInfo getApi() {
		return CacheUtil.get(AppInfo.class,"");
	}
	public void setWmToken(String wmAccessToken,Date wmTokenDate) throws Exception{
		AppInfo api=getApi();
		api.setWmAccessToken(wmAccessToken);
		api.setWmTokenDate(wmTokenDate);
		ModelUpdateUtil.updateModel(api, "wmAccessToken","wmTokenDate");
		CacheUtil.clear(AppInfo.class);
	}
	public void setWmTicket(String wmJsapiTicket,Date wmTicketDate) throws Exception{
		AppInfo api=getApi();
		api.setWmJsapiTicket(wmJsapiTicket);
		api.setWmTicketDate(wmTicketDate);
		ModelUpdateUtil.updateModel(api, "wmJsapiTicket","wmTicketDate");
		CacheUtil.clear(AppInfo.class);
	}
	public WxServerApiUtil(){}
	protected String getWmAccessToken() throws Exception{
		AppInfo api=getApi();
		if(StringUtil.isSpace(api.getWxAppId())||StringUtil.isSpace(api.getWxSecret())) throw new MException(this.getClass(),"未设置微信小程序appid和appkey");
		if(null==getApi().getWmTokenDate()||new Date().getTime()>getApi().getWmTokenDate().getTime()){
			JSONObject json=JSONObject.parseObject(new HttpRequestUtil().doGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" +
					api.getWxAppId() + "&secret=" + api.getWxSecret()));
			String access_token=json.getString("access_token");
			if(null==access_token){
				System.out.println(json);
				throw new MException(this.getClass(),json.getString("errmsg"));
			}else{
				setWmToken(access_token,new Date(new Date().getTime()+(json.getInteger("expires_in")-10)* 1000L));
			}
		}
		return getApi().getWmAccessToken();
	}
	private String getWmJsapiTicket(String access_token) throws Exception{
		if(null==getApi().getWmTicketDate()||new Date().getTime()>getApi().getWmTicketDate().getTime()){
			JSONObject json=JSONObject.parseObject(new HttpRequestUtil().doGet("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token +
					"&type=jsapi"));
			String jsapi_ticket=json.getString("ticket");
			if(null==jsapi_ticket){
				System.out.println(json);
				throw new MException(this.getClass(),json.getString("errmsg"));
			}else{
				setWmTicket(jsapi_ticket, new Date(new Date().getTime()+(json.getInteger("expires_in")-10)* 1000L));
			}
		}
		return getApi().getWmJsapiTicket();
	}
	//////////////////// 微信小程序
	public JSONObject getWmOpenInfo(String code) throws Exception {
		AppInfo api=getApi();
		return JSONObject.parseObject(new HttpRequestUtil().doGet("https://api.weixin.qq.com/sns/jscode2session?" +
			"appid=" + api.getWxAppId() + "&secret=" + api.getWxSecret() +
			"&js_code=" + code + "&grant_type=authorization_code"));
	}

	/**
	 * 获取微信小程序二维码
	 */
	public File getWmUnlimited(String page, String scene, int width, String path,String fileName) throws Exception {
		JSONMessage param=new JSONMessage();
		param.push("page", page);
		param.push("width", width);
		param.push("scene", scene);
		param.push("is_hyaline", true);
		return new HttpRequestUtil().download("https://api.weixin.qq.com/wxa/getwxacodeunlimit?" + "access_token=" + getWmAccessToken(),param.toJSONString(),path,fileName);
	}


}
